package com.gzw.scm.utils;

import android.graphics.Bitmap;
import android.os.Handler;

import com.gzw.scm.MyApplication;


public class FileUtil {
    private static FileUtil instance;
    private String isUpLoad="0";
    public static FileUtil getInstance() {
        if (instance == null) {
            instance = new FileUtil();
        }
        return instance;
    }
    public void uploadFile(final Bitmap mBitmap, final String newFileName) {
        String imgString = ToolFor9Ge.getBase64FromBitmap(mBitmap,100);
        uploadFile(imgString, newFileName);
    }
    private void uploadFile(final String imgString, final String newFileName) {
        new Thread() {
            @Override
            public void run() {
                udload(imgString, newFileName);
//                uploadPhoto(imgString, newFileName);
            }
        }.start();
    }
    public void udload(String imgString, String newFileName) {
        isUpLoad = "1";
        String json = "imgString=" + imgString + "&newFileName=" + newFileName + "";
        final String result = HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("getUploadImage"), json);
        new Handler(MyApplication.getmContext().getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (result.equals("")) {
                    T.showShort(MyApplication.getmContext(), "连接服务器异常");
                    isUpLoad = "0";
                } else {

                    if (result.indexOf("errorMsg") > 0) {
                        String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                        T.showShort(MyApplication.getmContext(), errorMsg);
                        isUpLoad = "0";
                    } else {
                        String successMsg = JsonUtil.getMsg(result, "successMsg");
                        T.showShort(MyApplication.getmContext(), JsonUtil.getMsg(result, "successMsg"));


                        if (JsonUtil.getMsg(result, "successMsg").equals("成功")) {
                            isUpLoad = "2";
                        } else {
                            isUpLoad = "0";
                        }
                        L.i("----------udload----------HttpUtils.doPost", result);
                    }
                }
            }
        });

    }
    public String isUpload(){
        return isUpLoad;
    }


}